package com.wsm.common.controller;

import com.wsm.common.annotation.OperateLog;
import com.wsm.common.domain.CloseDO;
import com.wsm.common.service.CloseService;
import com.wsm.common.utils.PageUtils;
import com.wsm.common.utils.Query;
import com.wsm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 开关表
 * 
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-08 12:27:30
 */
@Api(tags = "开关")
@Controller
@RequestMapping("/common/close")
public class CloseController {
	@Autowired
	private CloseService closeService;
	
	@GetMapping()
	@ApiOperation("跳转开关界面")
	@RequiresPermissions("common:close:close")
	String Close(){
	    return "common/close/close";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@ApiOperation("展示开关列表")
	@RequiresPermissions("common:close:close")
	public Result<PageUtils> list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CloseDO> closeList = closeService.list(query);
		int total = closeService.count(query);
		PageUtils pageUtils = new PageUtils(closeList, total);
		return Result.success(pageUtils);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("common:close:add")
	String add(){
	    return "common/close/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:close:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CloseDO close = closeService.get(id);
		model.addAttribute("close", close);
	    return "common/close/edit";
	}

	/**
	 * 保存
	 */
	@OperateLog("保存开关")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:close:add")
	public Result save(CloseDO close) {
		if (closeService.save(close)) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 修改
	 */
	@OperateLog("修改开关")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:close:edit")
	public Result update(CloseDO close) {
		if (closeService.update(close)) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 删除
	 */
	@OperateLog("删除开关")
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("common:close:remove")
	public Result remove(Long id, String model) {
		if (closeService.remove(id, model)) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 删除
	 */
	@OperateLog("批量删除开关")
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:close:batchRemove")
	public Result remove(@RequestParam("ids[]") Long[] ids) {
		closeService.batchRemove(ids);
		return Result.success();
	}

	@OperateLog("修改开关状态")
	@PostMapping(value = "/changeCloseStatus")
	@ResponseBody
	public Result changeCloseStatus(Long id, String model, String cmd) {
		String label = "0";
		if ("start".equals(cmd)) {
			label = "1";
		} else {
			label = "0";
		}
		if (closeService.changeStatus(id, model, label)) {
			return Result.success();
		} else {
			return Result.deny();
		}
	}
}
